package com.example.testactivity0406;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    private TextView textView;
    private static final String TAG ="" ;
    private static final String PREFS_NAME ="android.bignerdranch.s1" ;
    Button btnLogin,btnRegistered;
    EditText etAccount,etPassword ;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;//数据保存
    CheckBox cbRemember,cb_auto;//复选框
    int isRemember = 1;
    int iscb_auto=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin=findViewById(R.id.btn_Login);
        btnRegistered=findViewById(R.id.btn_Registered);
        etAccount=findViewById(R.id.account_input);
        etPassword=findViewById(R.id.password_input);
        cbRemember = findViewById(R.id.cb_Remember);
        cb_auto=findViewById(R.id.cb_auto);
        btnLogin.setOnClickListener(this);
        btnRegistered.setOnClickListener(this);
        Init();
    }

    void Init(){
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        if(sharedPreferences.getInt("isRemember",0) == 1) {
            etAccount.setText(sharedPreferences.getString("user_name", null));
            etPassword.setText(sharedPreferences.getString("password", null));
        }
        if(sharedPreferences.getInt("iscb_auto",0) == 1) {
            etAccount.setText(sharedPreferences.getString("user_name", null));
            etPassword.setText(sharedPreferences.getString("password", null));
        }
        if (cb_auto.isChecked()) {
            iscb_auto = 1;
            editor.putInt("iscb_auto", iscb_auto);
            Toast.makeText(MainActivity.this,"自动登录成功,即将为您跳转到用户界面",Toast.LENGTH_SHORT).show();
        } else {
            iscb_auto = 0;
            editor.putInt("iscb_auto", iscb_auto);
        }
        if (cbRemember.isChecked()) {
            isRemember = 1;
            editor.putInt("isRemember", isRemember);
        }
        editor.apply();

    }

    /**
     * 清空数据
     */
    private void clearUserInfo(){
        SharedPreferences userInfo = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        editor.clear();
        editor.apply();
        Log.i(TAG, "清空数据");
    }

    @Override
    public void onClick(View v) {
        final int btn_Login = R.id.btn_Login;
        final int btnRegistered = R.id.btn_Registered;
        switch (v.getId()) {
            case btn_Login:{
                String name = sharedPreferences.getString("user_name",null);
                String pass = sharedPreferences.getString("password",null);
                if(etAccount.getText().toString().equals(name) && etPassword.getText().toString().equals(pass))
                    Toast.makeText(MainActivity.this,"登录成功,即将为您跳转到用户界面",Toast.LENGTH_SHORT).show();
                else{
                    Toast.makeText(MainActivity.this,"登录失败，请检查用户名或者密码是否输入正确",Toast.LENGTH_SHORT).show();
                }
                if (cb_auto.isChecked()) {
                    iscb_auto = 1;
                    editor.putInt("iscb_auto", iscb_auto);
                    Toast.makeText(MainActivity.this,"自动登录成功,即将为您跳转到用户界面",Toast.LENGTH_SHORT).show();
                }

                if (cbRemember.isChecked()) {
                    isRemember = 1;
                    editor.putInt("isRemember", isRemember);
                } else {
                    isRemember = 0;
                    editor.putInt("isRemember", isRemember);
                }
                editor.apply();
                break;
            }
            case btnRegistered:{
                editor.putString("user_name",etAccount.getText().toString());
                editor.putString("password",etPassword.getText().toString());
                editor.apply();
                Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                break;
            }

        }
    }


}